package com.yys.apm.model;

import com.yys.apm.boot.ApmAgentManager;
import com.yys.apm.config.ApmConfig;
import com.yys.apm.constant.CommonConstant;
import com.yys.apm.utils.ApmAgentUtil;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author Yingsheng Ye
 * @date 2021-03-20 11:02
 */
public class CollectInfo extends HashMap<String, Object> implements java.io.Serializable {

    //应用ID
    private String appId;

    //主机地址
    private String hostIp = ApmAgentUtil.getHostIp();

    //主机名称
    private String hostName = ApmAgentUtil.getHostName();

    //记录信息
    private String msg;

    //状态 1成功 0失败
    private Integer status = CommonConstant.SUCCESS;

    //traceId
    private String traceId;

    //parentId
    private String parentId;

    //spanId
    private String spanId;

    //请求地址
    private String requestUrl;

    //执行类名
    private String className;

    //执行方法名
    private String methodName;

    //类型
    private String type;

    //用户访问标示
    private String visitUserId;

    //开始时间
    private Long startTime;

    //结束时间
    private Long endTime;

    //耗时
    private Long costTime;

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getSpanId() {
        return spanId;
    }

    public void setSpanId(String spanId) {
        this.spanId = spanId;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public String getVisitUserId() {
        return visitUserId;
    }

    public void setVisitUserId(String visitUserId) {
        this.visitUserId = visitUserId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public Long getEndTime() {
        return endTime;
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }

    public Long getCostTime() {
        return costTime;
    }

    public void setCostTime(Long costTime) {
        this.costTime = costTime;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getHostIp() {
        return hostIp;
    }

    public void setHostIp(String hostIp) {
        this.hostIp = hostIp;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }


    public CollectInfo builder(HttpServletRequest httpServletRequest, String type, String traceId, String spanId, boolean setSpanId) {
        this.type = type;
        if (StringUtils.isEmpty(traceId)) {
            traceId = ApmAgentUtil.generateId();
            if (null != httpServletRequest) {
                httpServletRequest.setAttribute(CommonConstant.TRACE_ID, traceId);
            }
        }
        this.traceId = traceId;
        if (!StringUtils.isEmpty(spanId)) {
            this.parentId = spanId;
        }
        spanId = ApmAgentUtil.generateId();
        if (setSpanId && null != httpServletRequest) {
            httpServletRequest.setAttribute(CommonConstant.SPAN_ID, spanId);
        }
        this.spanId = spanId;
        startTime = System.currentTimeMillis();
        if (null != httpServletRequest) {
            this.setRequestUrl(httpServletRequest.getRequestURL().toString());
            this.setVisitUserId((String) httpServletRequest.getAttribute(ApmAgentManager.getInstance().properties.getProperty(ApmConfig.GET_USER_ID_TARGET)));
        }
        this.appId = ApmAgentManager.getInstance().properties.getProperty(ApmConfig.APP_ID);
        return this;
    }

    public static CollectInfo build() {
        return new CollectInfo();
    }


    private CollectInfo() {
    }

    @Override
    public String toString() {
        return "CollectInfo{" +
                "appId='" + appId + '\'' +
                ", hostIp='" + hostIp + '\'' +
                ", hostName='" + hostName + '\'' +
                ", msg='" + msg + '\'' +
                ", status=" + status +
                ", traceId='" + traceId + '\'' +
                ", parentId='" + parentId + '\'' +
                ", spanId='" + spanId + '\'' +
                ", requestUrl='" + requestUrl + '\'' +
                ", className='" + className + '\'' +
                ", methodName='" + methodName + '\'' +
                ", type='" + type + '\'' +
                ", visitUserId='" + visitUserId + '\'' +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", costTime=" + costTime +
                '}';
    }
}
